<template>
  <div style="padding: 10px;">
    <Row :space="15">
      <Cell>
        <Input v-model="formData.parameterName" placeholder="参数名称" style="width: 100%;" />
      </Cell>
      <Cell>
        <Input v-model="formData.name" placeholder="节点名称" style="width: 100%;" />
      </Cell>
      <Cell>
        <Input v-model="formData.url" placeholder="请求地址" style="width: 100%;" />
      </Cell>
      <Cell>
        <Select v-model="formData.contentType" :datas="contentTypes" keyName="value" titleName="label"
          placeholder="请求类型"></Select>
      </Cell>
      <Cell>
        <Select v-model="formData.method" :datas="methods" keyName="value" titleName="label"
          placeholder="请求方式"></Select>
      </Cell>
      <Cell>
        <Input v-model="formData.headers" placeholder="Header" style="width: 100%;" />
      </Cell>
      <Cell>
        <Input v-model="formData.cookies" placeholder="Cookie" style="width: 100%;" />
      </Cell>
      <Cell>
        <AutoComplete v-model="formData.body" :mustMatch="false" type="title" :datas="parameters" placeholder="Body" />
      </Cell>
      <Cell>
        <div class="h-input-group" style="width: 400px">
          <div class="h-input-addon">
            <Input v-model="formData.validateKey" style="width: 100px;" />
          </div>
          <div class="h-input-addon" style="width: 100px;">
            <Select v-model="formData.expression" :datas="expressions" :no-border="true" :deletable="false"></Select>
          </div>
          <div class="h-input-addon">
            <Input v-model="formData.validateValue" style="width: 176px;" />
          </div>
        </div>
      </Cell>
      <Cell>
        <Input v-model="formData.dataKey" placeholder="保留结果：data || result" style="width: 100%;" />
      </Cell>
      <Cell>
        <Checkbox v-model="formData.printInput" placeholder="跟踪打印" title="跟踪打印">打印输入</Checkbox>
        <Space :width="15" />
        <Checkbox v-model="formData.printOutput" title="跟踪打印">打印输出</Checkbox>
        <Space :width="15" />
        <Checkbox v-model="formData.inputHistory" title="会话历史">保存输入</Checkbox>
        <Space :width="15" />
        <Checkbox v-model="formData.resultHistory" title="会话历史">保存输出</Checkbox>
      </Cell>

      <Cell>
        <Button color="primary" @click="bindForm">设置</Button>
        <Space :width="15" />
        <Button @click="setForm" color="yellow">重置</Button>
      </Cell>
    </Row>
  </div>
</template>

<script>
import LogicFlow from '@logicflow/core';
import { message } from 'heyui';

export default {
  props: {
    lf: Object,
    currentNode: Object,
  },
  mounted() {
    this.setForm();
    this.getParameters();
  },
  data() {
    return {
      expressions: [
        { label: "等于", value: "=" },
        { label: "不等于", value: "!=" },
        { label: "大于", value: ">" },
        { label: "大于等于", value: ">=" },
        { label: "小于", value: "<" },
        { label: "小于等于", value: "<=" },
        { label: "包含", value: "include" },
        { label: "不包含", value: "exclusive" },
        { label: "以*开始", value: "startWith" },
        { label: "以*结束", value: "endWith" },
      ],
      contentTypes: [
        { label: "application/json", value: "application/json" },
        { label: "application/x-www-form-urlencoded", value: "application/x-www-form-urlencoded" },
        { label: "multipart/form-data", value: "multipart/form-data" },
        { label: "text/html", value: "text/html" },
      ],
      methods: [
        { label: "GET", value: "GET" },
        { label: "POST", value: "POST" },
        { label: "PUT", value: "PUT" },
        { label: "DELETE", value: "DELETE" },
      ],
      parameters: [],
      formData: {
        validateKey: "code",
        expression: "=",
        validateValue: "200",
        dataKey: "",
        method: "POST",
        contentType: "application/json",
        resultHistory: true,
      }
    }
  },
  methods: {
    setForm() {
      this.formData.name = this.$props.currentNode.data.text.value;
      let id = this.$props.currentNode.data.id;
      let properties = this.$props.lf.getProperties(id);
      this.formData.printInput = properties.printInput;
      this.formData.printOutput = properties.printOutput;
      this.formData.url = properties.url;
      if (!!properties.contentType) {
        this.formData.contentType = properties.contentType;
      }
      if (!!properties.method) {
        this.formData.method = properties.method;
      }
      if (!!properties.dataKey) {
        this.formData.dataKey = properties.dataKey;
      }
      if (!!properties.validateKey) {
        this.formData.validateKey = properties.validateKey;
      }
      if (!!properties.expression) {
        this.formData.expression = properties.expression;
      }
      if (!!properties.validateValue) {
        this.formData.validateValue = properties.validateValue;
      }
      this.formData.headers = properties.headers;
      this.formData.cookies = properties.cookies;
      this.formData.body = properties.body;

      this.formData.parameterName = properties.parameterName;
      this.formData.inputHistory = properties.inputHistory;
      if (!!properties.resultHistory) {
        this.formData.resultHistory = properties.resultHistory;
      }
    },
    bindForm() {
      let id = this.$props.currentNode.data.id;
      this.$props.lf.updateText(id, this.formData.name);
      this.$props.lf.setProperties(id, this.formData);
      this.$emit('close', false);
      message.success("设置成功");
    },
    getModel() {
      this.$get("/chat/getModel").then(res => {
        if (res.data.code == this.$success) {
          this.models = res.data.data;
        } else {
          message.error(res.data.msg);
        }
      });
    },
    getFilenameList() {
      this.$post("/ollamaembedding/list", { vars: { "a.collection_id": this.formData.collectionId }, page: { current: 1, size: 1000 } }).then(res => {
        if (res.data.code == this.$success) {
          this.filenameList = res.data.data.rows;
        } else {
          this.filenameList = [];
          message.error(res.data.msg);
        }
      })
    },
    getCollectionList() {
      this.$post("/ollamacollection/list", { page: { current: 1, size: 1000 } }).then(res => {
        if (res.data.code == this.$success) {
          this.collectionList = res.data.data.rows;
          if (!!this.collectionId) {
            this.getFilenameList();
          }
        } else {
          message.error(res.data.msg);
        }
      })
    },
    collectionChange(data) {
      this.filename = "";
      this.formData.collectionName = data.label;
      this.getFilenameList(data.value);
    },

    getParameters() {
      let flowData = (this.$props.lf).getGraphRawData();
      let nodes = flowData["nodes"];
      let node = this.getStartNode(flowData);
      while (!!node && node?.id != this.$props.currentNode?.data?.id && node?.type != 'EndNode') {
        var properties = node?.properties;
        var name = properties?.name;
        var parameterName = properties?.parameterName;

        if (!!parameterName) {
          if (properties?.inputHistory == true) {
            this.parameters.push({ label: "$" + parameterName + ".input", value: "$" + parameterName + ".input" });
          } if (properties?.resultHistory == true) {
            this.parameters.push({ label: "$" + parameterName + ".output", value: "$" + parameterName + ".output" });
          }
        }
        node = this.getNextNode(flowData, node.id);
      }
    },
    getStartNode(flowData) {
      let nodes = flowData["nodes"];
      for (var i = 0; i < nodes.length; i++) {
        let node = nodes[i];
        if (node.type == 'StartNode') {
          return node;
        }
      }
    },
    getNode(flowData, id) {
      let nodes = flowData["nodes"];
      for (var i = 0; i < nodes.length; i++) {
        let node = nodes[i];
        if (node.id == id) {
          return node;
        }
      }
    },
    getNextNode(flowData, id) {
      let edges = flowData["edges"]
      for (var i = 0; i < edges.length; i++) {
        let edge = edges[i];
        if (edge.sourceNodeId == id) {
          return this.getNode(flowData, edge.targetNodeId);
        }
      }
    },
  }
}
</script>